<?php

/**
 * 支付单
 */
class Application_Model_PayBill extends Application_Model_DomainObject {

    //相关帐户
    protected $_accountId;
    //人民币金额
    protected $_money;
    //ID
    protected $_id;
    //支付状态，参考Application_Model_Status
    protected $_status;
    //支付类型，参考PayController里面的常量
    protected $_type;
    //创建日期
    protected $_dateCreated;
    //支付方式ID
    protected $_payId;
    //产生支付原因相关对象ID
    protected $_domainId;
    //支付描述
    protected $_description;
    //美元
    protected $_usmoney;
    //汇率
    protected $_rate;
    //汇兑损失率
    protected $_discountRate;
    //充值交易号
    protected $_txnId;

    public static function isExistTxnId($id) {
        return Application_Model_PayBill::getDao()->queryOne(array('txnId' => $id)) != NULL;
    }

    /**
     * 新创建
     */

    const PAY_STATUS_CREATED = 0;

    /**
     * 支付成功
     */
    const PAY_STATUS_SUCCESS = 1;

    /**
     * 到了支付网站而没支付
     */
    const PAY_STATUS_GOPAY = 2;

    /**
     * 买家已经确定付款
     */
    const PAY_STATUS_FINISH = 3;

    /**
     * 买家已经确定付款
     */
    const PAY_STATUS_BACK = 4;
    //=========支付类型============

    /**
     * 充值 
     */
    const TYPE_RECHANGE = 1;

    /**
     * 代购订单 
     */
    const TYPE_ORDER = 2;

    /**
     * 国际运单 
     */
    const TYPE_WAYBILL = 3;

    /**
     * 代付款 
     */
    const TYPE_AGENT_PAY = 4;

    /**
     * 充值码 
     */
    const TYPE_ALI_CODE = 5;

    /**
     * 移动充值卡 
     */
    const TYPE_CARD = 6;

    /**
     * 代充值 
     */
    const TYPE_AGENT_RECHANGE = 7;
    /**
     * 上门取件
     */
    const TYPE_Visiting = 8;

    /**
     * 补交款链接
     */
    const TYPE_REPAY_LINK = 9;
    
    /**
     * 支付宝 
     */
    const PAY_ALIPAY = 2;
    /**
     * Paypal支付 
     */
    const PAY_PAYPAL = 1;
    /**
     * 使用帐户余额支付 
     */
    const PAY_ACCOUNT = 3;

    /**
     * 使用国际信用卡
     */
    const PAY_CARD = 4;

     /**
     * dunhuang支付 
     */
    const PAY_DUNHUANG = 5;

    public static function getStatusText($status) {
        $map = array(NULL => '未支付'
            , Application_Model_PayBill::PAY_STATUS_CREATED => '未支付'
            , Application_Model_PayBill::PAY_STATUS_FINISH => '已完成'
            , Application_Model_PayBill::PAY_STATUS_GOPAY => '未支付'
            , Application_Model_PayBill::PAY_STATUS_SUCCESS => '支付成功，等待受理'
            , Application_Model_PayBill::PAY_STATUS_BACK => '悠奇退款');
        return $map[$status];
    }

    /**
     * 返回类型名称
     * @param type $type
     * @return string 
     */
    public static function getTypeName($type) {
        $map = array(Application_Model_PayBill::TYPE_RECHANGE => '充值'
            , Application_Model_PayBill::TYPE_ORDER => '代购订单'
            , Application_Model_PayBill::TYPE_WAYBILL => '国际运音'
            , Application_Model_PayBill::TYPE_AGENT_PAY => '代支付'
            , Application_Model_PayBill::TYPE_ALI_CODE => '支付宝充值码'
            , Application_Model_PayBill::TYPE_CARD => '移动充值卡'
            , Application_Model_PayBill::TYPE_AGENT_RECHANGE => '代充值'
            , Application_Model_PayBill::TYPE_Visiting => '上门取件'
            , Application_Model_PayBill::TYPE_REPAY_LINK => '补交款');

        return $map[$type];
    }

    public static function getFilter($paybill, $pagesize, $pageindex) {
//        var_dump($paybill->getSearchFileds());exit;
        return Application_Model_PayBill::getDao()->queryPage($paybill->getSearchFileds(), "dateCreated desc", $pagesize, $pageindex);
    }

    public function getuserinfo() {
        if ($this->_accountId) {
            return Application_Model_User::load($this->_accountId);
        }
    }

    public function getpayname() {
        if ($this->_payId != "") {
            switch ($this->_payId) {
                case 1:
                    return "Paypal支付";
                    break;
                case 2:
                    return "支付宝支付";
                    break;
                case 3:
                    return "帐户余额支付";
                    break;
                case 4:
                    return "国际信用卡支付";
                    break;
                case 7:
                    return "微信支付";
                    break;
                default:
                    break;
            }
        }
    }

    public static function bypayIdTxnId($payId,$txnId)
    {
        $where = array();
        $and = "";
        if($payId != ""){
            $where[" payId = "] = $payId;
            $and = " and ";
        }
        if($txnId != ""){
            $where[$and." txnId = "] = $txnId;
            $and = " and ";
        }
        return Application_Model_PayBill::getDao()->queryOne($where);
    }

    public static function getCardBill($userid,$domainId,$typeId)
    {
        $where = array();
        $and = "";
        if($userid != ""){
            $where[" accountId = "] = $userid;
            $and = " and ";
        }
        if(Application_Model_PayBill::PAY_CARD != ""){
            $where[$and." payId = "] = Application_Model_PayBill::PAY_CARD;
            $and = " and ";
        }
        if($domainId != ""){
            $where[$and." domainId = "] = $domainId;
            $and = " and ";
        }
        if($typeId != ""){
            $where[$and." type = "] = $typeId;
            $and = " and ";
        }
        if(!Application_Model_PayBill::getDao()->queryAll($where)){
            return NULL;
        }else{
            $where1 = array();
            $and = "";
            if($userid != ""){
                $where1[" accountId = "] = $userid;
                $and = " and ";
            }
            if(Application_Model_PayBill::PAY_CARD != ""){
                $where1[$and." payId = "] = Application_Model_PayBill::PAY_CARD;
                $and = " and ";
            }
            
            $where1[$and." status = "] = 1;
            $and = " and ";
            
            return Application_Model_PayBill::getDao()->queryAll($where1,'dateCreated asc');
        }
        
    }

    public function getstatusname() {
        if ($this->_status != "") {
            switch ($this->_status) {
                case Application_Model_PayBill::PAY_STATUS_CREATED:
                    return "未支付";
                    break;
                case Application_Model_PayBill::PAY_STATUS_SUCCESS:
                    return "已支付";
                    break;
                case Application_Model_PayBill::PAY_STATUS_GOPAY:
                    return "未支付";
                    break;
                case Application_Model_PayBill::PAY_STATUS_FINISH:
                    return "卖家已确认支付";
                    break;
                default:
                    return "未知状态（$this->_status）";
            }
        }
    }

    public function gettypenames() {
        if ($this->_type != "") {
            switch ($this->_type) {
                case Application_Model_PayBill::TYPE_RECHANGE:
                    return "充值";
                    break;
                case Application_Model_PayBill::TYPE_ORDER:
                    return "代购订单";
                    break;
                case Application_Model_PayBill::TYPE_WAYBILL:
                    return "国际运单";
                    break;
                case Application_Model_PayBill::TYPE_AGENT_PAY:
                    return "代支付";
                    break;
                case Application_Model_PayBill::TYPE_ALI_CODE:
                    return "支付宝充值码";
                    break;
                case Application_Model_PayBill::TYPE_CARD:
                    return "移动充值卡";
                    break;
                case Application_Model_PayBill::TYPE_AGENT_RECHANGE:
                    return "代充值";
                    break;
                case Application_Model_PayBill::TYPE_Visiting:
                    return "上门取件";
                    break;
                case Application_Model_PayBill::TYPE_REPAY_LINK:
                    return '补交款';
                    break;
                default:
                    return "未知类型($this->_type)";
                    break;
            }
        }
    }
    
        
    /**
     * 返回一个支付单
     * @param type $type
     * @param type $id
     * @return Application_Model_PayInterface 
     */
    public static function createPayer($type,$id){
        switch ($type){
            case self::TYPE_RECHANGE:
                return Application_Model_RechargeBill::load($id);
                break;
            case Application_Model_PayBill::TYPE_ORDER:
                return Application_Model_Order::createByOrderIds($id);
                break;
            case Application_Model_PayBill::TYPE_WAYBILL:
                return Application_Model_wayBillItem::load($id);
                break;
            case Application_Model_PayBill::TYPE_AGENT_PAY:
                return Application_Model_AgentPay::load($id);
                break;
            case Application_Model_PayBill::TYPE_AGENT_RECHANGE:
                return Application_Model_topUpAlipay::load($id);
                break;
            case Application_Model_PayBill::TYPE_ALI_CODE:
                return Application_Model_PayCodeOrder::load($id);
                break;
            case Application_Model_PayBill::TYPE_CARD:
                return Application_Model_PayCodeOrder::load($id);
                break;
            case Application_Model_PayBill::TYPE_Visiting:
                return Application_Model_VisitingServer::load($id);
                break;
            case Application_Model_PayBill::TYPE_REPAY_LINK:
                return Application_Model_RepayLink::load($id);
                break;
        }        
    }

    public function createPay(){
        return self::createPayer($this->_type, $this->_domainId);
    }
    
    public static function getFilterAll($paybill)
    {
//          var_dump($paybill->getSearchFileds());exit;
          return self::getDao()->queryAll($paybill->getSearchFileds());
    }
    
    public function getPayFormula()
    {
        switch ($this->_payId) {
            case self::PAY_PAYPAL:
                return "[$this->_money(该付人民币)+$this->_money*3%(悠奇汇兑手续费)]/$this->_rate(汇率)+0.3(Paypal手续费：USD)=$this->_usmoney(实付美元)";
            case self::PAY_ALIPAY:
                return "$this->_money";
            case self::PAY_ACCOUNT:
                return "$this->_money";
            case self::PAY_CARD:
                return "[$this->_money(该付人民币)+$this->_money*4%(悠奇汇兑手续费)]/$this->_rate(汇率)=$this->_usmoney(实付美元)";
            default:
                break;
        }
    }

}